/**
* @Author: zmx
* @Date: 2023/9/26
* @Description:
*/
<template>
  <el-dialog :title="titleFront + '风险评估算法指标'" :visible.sync="show" append-to-body width="50%" :before-close="handleClose">
    <el-form :model="formInline">
      <el-form-item label="算法指标名称：">
        <span v-if="formInline.type === 'detail'">{{formInline.itemName}}</span>
        <el-input v-else v-model="formInline.itemName" placeholder="请输入算法指标名称" maxlength="100" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="对象类型：">
        <el-select
          v-model="formInline.itemType"
          value-key="id"
          filterable
          :disabled="formInline.type === 'detail'"
        >
          <el-option
            v-for="item in optionsList"
            :key="item.id"
            :label="item.itemObjectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="算法指标符号：">
        <span v-if="formInline.type === 'detail'">{{formInline.itemTypeSymbol}}</span>
        <el-input v-else v-model="formInline.itemTypeSymbol" placeholder="请输入算法指标符号" @change="changeItemTypeSymbol" maxlength="10" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="算法指标描述：">
        <span v-if="formInline.type === 'detail'">{{formInline.itemDesc}}</span>
        <el-input v-else v-model="formInline.itemDesc" placeholder="请输入算法指标描述" maxlength="200" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="维护建议：">
        <span v-if="formInline.type === 'detail'">{{formInline.maintainAdvice}}</span>
        <el-input v-else v-model="formInline.maintainAdvice" placeholder="请输入维护建议" maxlength="200" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="采集表名：">
        <span v-if="formInline.type === 'detail'">{{formInline.crawlTable}}</span>
        <el-input v-else v-model="formInline.crawlTable" placeholder="请输入采集表名" maxlength="200" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="采集字段：">
        <span v-if="formInline.type === 'detail'">{{formInline.crawlColumn}}</span>
        <el-input v-else v-model="formInline.crawlColumn" placeholder="请输入采集字段" maxlength="200" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="checkBtn" v-if="formInline.type !== 'detail'">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getItemDictObject} from "../../api/risk/commonDict"

export default {
  props: ['ItemDialogShow', 'itemParam'], //接受父组件传递过来的数据
  data() {
    return {
      show: false, //弹窗默认隐藏
      formInline: {
        itemName:'',
        itemType:'',
        itemTypeSymbol: '',
        itemDesc: ''
      },
      optionsList:[],
      titleFront: '查看'
    };
  },
  mounted() {
    //显示弹窗
    this.getInit()
    this.formInline = JSON.parse(JSON.stringify(this.itemParam))
    if (this.formInline.type === 'add') {
      this.titleFront = '新建'
    } else if (this.formInline.type === 'edit') {
      this.titleFront = '编辑'
    }
    this.show = this.ItemDialogShow
  },
  methods: {
    getInit() {
      getItemDictObject().then((res)=>{
        if(res){
          this.optionsList = res
        }
      }).catch(err => {
        this.$message({ message: '服务器异常：' + err, type: 'warning' })
      })
    },
    handleClose() {
      //关闭弹窗
      this.show = false
      this.$emit('closeDialog')
    },
    checkBtn() {
      //确定按钮
      if (this.checkParam()) {
        this.$emit('selectData', this.formInline)   //发送数据到父组件
        this.handleClose()
      }
    },
    changeItemTypeSymbol() {
      let reg = /[A-Za-z]\w*/
      if (!this.formInline.itemTypeSymbol.match(reg)) {
        this.formInline.itemTypeSymbol = ''
        this.$message({ message: '算法指标符号以字母开头,数字和字母组成', type: 'warning' })
      }
    },
    checkParam() {
      if (!this.formInline.itemName) {
        this.$message({ message: '请输入算法指标名称', type: 'warning' })
        return false
      }
      if (!this.formInline.itemType) {
        this.$message({ message: '请选择对象类型', type: 'warning' })
        return false
      }
      if (!this.formInline.itemTypeSymbol) {
        this.$message({ message: '请输入算法指标符号', type: 'warning' })
        return false
      }
      if (!this.formInline.itemDesc) {
        this.$message({ message: '请输入算法指标描述', type: 'warning' })
        return false
      }
      return true
    }
  }
};
</script>

<style scoped>

</style>
